
import default_components from "../config/component_list";
import { objectAssign } from "./common";

/**
 * PopoForm AutoView 小插件 方法返回的事件对象
 */
function PopoAutoView( popoForm){

    var ins = this;
    this.popoForm = popoForm;
    this.disabledProcessButtonSource = null;
    this.currentMode = "";
    this.storeFormData = {};

    /**
     * 在 PopoForm open函数调用前，先对 popoForm.config 配置进行预处理。
     */
    this.onOpenStart = function(){

        var form = ins.popoForm;

        var config = form.config;
        form.is_auto_view = config.autoView !== undefined ? config.autoView: form.autoView;
        form.current_auto_view_mode = config.currentAutoViewMode !== undefined ? config.currentAutoViewMode : form.current_auto_view_mode; 
        form.view_to_edit_button_text = config.viewToEditButtonText|| "编辑";

        // console.log( "is auto view", form.is_auto_view, form.current_auto_view_mode);
        if( !!form.is_auto_view){
            ins.storeValue();
            ins.initAuto();
            form.getPopoEvent().on( "cancel", ins.handleCancel);
        }
    }

    /**
     * 保存当前表单里各个元素的value的值
     */
    this.storeValue = function(){
        var form = ins.popoForm;
        this.storeFormData = objectAssign( {}, form.formData);
    }

    /**
     * 将表单里各个元素的value还原成 storeValue 中所保存的内容
     */
    this.releaseValue = function(){
        var form = ins.popoForm;
        form.formData = this.storeFormData;
    }

    this.initAuto = function(){
        var form = ins.popoForm;
        var config = form.config;
        var current_mode = form.current_auto_view_mode;

        // console.log( current_mode);
        ins.currentMode = current_mode;

        var isView = current_mode == "view";
        ins.disabledProcessButtonSource = form.formProps.disabledProcessButton;
        form.formProps.disabledProcessButton = isView;
        var component_list = config.component_list;

        if( isView){
            ins.popoForm.addButton({
                text: form.view_to_edit_button_text,
                __id : "autoViewBtn",
                click: function(){
                    ins.storeValue();
                    form.current_auto_view_mode = "edit";
                    ins.initAuto();
                }
            });
        }else{
            ins.popoForm.removeButton( "autoViewBtn");
            // var index = ins.popoForm.customButtons.findIndex(  (btn)=>{
            //     return btn.__id == "autoViewBtn";
            // });
            // if( index >= 0){
            //     ins.popoForm.customButtons.splice( index, 1);
            // }
        }

        component_list.forEach( (cmpt)=>{

            // 记录原本在配置文件里面设定的组件是否使用view模式的设置。
            // 这样可以实现在编辑模式下，部分组件可以以view模式进行展现。
            if( cmpt.props.__aboriginal_use_view === undefined){
                cmpt.props.__aboriginal_use_view = !!cmpt.props.useView;
            }

            var cmptUseView = isView || cmpt.props.__aboriginal_use_view;
            cmpt.props.useView = cmptUseView;

            if( typeof cmpt._component_source == "string"){
                var component = default_components[ cmptUseView ? "view" : "edit"][ cmpt._component_source];
                if( component == null){
                    component = default_components.common[ cmpt._component_source];
                }
                cmpt.component = component;
            }
        });
    }

    this.handleCancel = function(){
        var form = ins.popoForm;
        var config = form.config;
        form.current_auto_view_mode = "view";
        ins.currentMode = null;
        if( !form.form_use_dialog ){
            ins.releaseValue();
            ins.initAuto();
        }
    }
}

export default PopoAutoView;